const Mock = require('mockjs')

// 模拟AI方案数据
const schemeList = Mock.mock({
  'items|20': [{
    'id|+1': 1,
    'title': '@ctitle(10, 20)',
    'type': '@pick(["single", "double", "triple"])',
    'expert_id|1-5': 1,
    'expert_name': '@cname',
    'match_count|1-5': 1,
    'status': '@pick(["pending", "approved", "rejected", "published"])',
    'content': '@cparagraph(3, 5)',
    'remark': '@cparagraph(1, 2)',
    'created_at': '@datetime',
    'updated_at': '@datetime'
  }],
  'total': 20
})

// 模拟专家数据
const expertList = Mock.mock({
  'items|10': [{
    'id|+1': 1,
    'name': '@cname',
    'type': '@pick(["conservative", "aggressive", "balanced"])',
    'specialty_leagues': '@pick(["英超,西甲", "德甲,意甲", "法甲,欧冠", "欧联,英超"])',
    'win_rate|60-85': 1,
    'description': '@cparagraph(2, 4)',
    'single_example': '@cparagraph(10, 15)',
    'double_example': '@cparagraph(10, 15)',
    'triple_example': '@cparagraph(10, 15)',
    'status': '@pick(["active", "inactive"])',
    'created_at': '@datetime',
    'updated_at': '@datetime'
  }],
  'total': 10
})

// 模拟赛事数据
const matchList = Mock.mock({
  'items|30': [{
    'id|+1': 1,
    'league': '@pick(["英超", "西甲", "德甲", "意甲", "法甲", "欧冠", "欧联"])',
    'home_team': '@cword(2, 4)',
    'away_team': '@cword(2, 4)',
    'match_time': '@datetime',
    'status': '@pick(["upcoming", "live", "finished", "cancelled"])',
    'win_odds|1.5-3.5': 1,
    'draw_odds|2.8-3.5': 1,
    'lose_odds|1.5-3.5': 1,
    'created_at': '@datetime',
    'updated_at': '@datetime'
  }],
  'total': 30
})

module.exports = [
  // AI方案管理接口
  {
    url: '/api/ai-scheme/list',
    type: 'get',
    response: config => {
      const { page = 1, limit = 20, title, status, type } = config.query
      let filteredList = schemeList.items
      
      if (title) {
        filteredList = filteredList.filter(item => item.title.includes(title))
      }
      if (status) {
        filteredList = filteredList.filter(item => item.status === status)
      }
      if (type) {
        filteredList = filteredList.filter(item => item.type === type)
      }
      
      const start = (page - 1) * limit
      const end = start + parseInt(limit)
      
      return {
        code: 200,
        data: {
          items: filteredList.slice(start, end),
          total: filteredList.length
        }
      }
    }
  },
  
  {
    url: '/api/ai-scheme/create',
    type: 'post',
    response: config => {
      const data = JSON.parse(config.body)
      const newScheme = {
        id: schemeList.items.length + 1,
        ...data,
        expert_name: expertList.items.find(e => e.id === data.expert_id)?.name || '未知专家',
        match_count: Math.floor(Math.random() * 5) + 1,
        status: 'pending',
        created_at: new Date().toISOString(),
        updated_at: new Date().toISOString()
      }
      schemeList.items.unshift(newScheme)
      schemeList.total += 1
      
      return {
        code: 200,
        data: newScheme
      }
    }
  },
  
  {
    url: '/api/ai-scheme/update',
    type: 'put',
    response: config => {
      const data = JSON.parse(config.body)
      const index = schemeList.items.findIndex(item => item.id === data.id)
      if (index !== -1) {
        schemeList.items[index] = { ...schemeList.items[index], ...data, updated_at: new Date().toISOString() }
      }
      
      return {
        code: 200,
        data: schemeList.items[index]
      }
    }
  },
  
  {
    url: '/api/ai-scheme/delete/([0-9]+)',
    type: 'delete',
    response: config => {
      const id = parseInt(config.url.split('/').pop())
      const index = schemeList.items.findIndex(item => item.id === id)
      if (index !== -1) {
        schemeList.items.splice(index, 1)
        schemeList.total -= 1
      }
      
      return {
        code: 200,
        message: '删除成功'
      }
    }
  },
  
  {
    url: '/api/ai-scheme/audit',
    type: 'post',
    response: config => {
      const data = JSON.parse(config.body)
      const index = schemeList.items.findIndex(item => item.id === data.scheme_id)
      if (index !== -1) {
        schemeList.items[index].status = data.result
        schemeList.items[index].audit_comment = data.comment
        schemeList.items[index].updated_at = new Date().toISOString()
      }
      
      return {
        code: 200,
        message: '审核完成'
      }
    }
  },
  
  {
    url: '/api/ai-scheme/publish/([0-9]+)',
    type: 'post',
    response: config => {
      const id = parseInt(config.url.split('/').pop())
      const index = schemeList.items.findIndex(item => item.id === id)
      if (index !== -1) {
        schemeList.items[index].status = 'published'
        schemeList.items[index].updated_at = new Date().toISOString()
      }
      
      return {
        code: 200,
        message: '发布成功'
      }
    }
  },
  
  {
    url: '/api/ai-scheme/detail/([0-9]+)',
    type: 'get',
    response: config => {
      const id = parseInt(config.url.split('/').pop())
      const scheme = schemeList.items.find(item => item.id === id)
      if (scheme) {
        // 模拟包含的赛事数据
        scheme.matches = matchList.items.slice(0, scheme.match_count).map(match => ({
          ...match,
          prediction: '@pick(["胜", "平", "负"])',
          odds: '@float(1.5, 3.5, 2, 2)'
        }))
      }
      
      return {
        code: 200,
        data: scheme
      }
    }
  },
  
  {
    url: '/api/ai-scheme/generate',
    type: 'post',
    response: config => {
      const data = JSON.parse(config.body)
      const generatedSchemes = []
      
      for (let i = 0; i < data.count; i++) {
        const scheme = {
          id: schemeList.items.length + i + 1,
          title: data.title || `AI生成方案${i + 1}`,
          type: data.type,
          expert_id: data.expert_id,
          expert_name: expertList.items.find(e => e.id === data.expert_id)?.name || '未知专家',
          match_count: data.type === 'single' ? 1 : data.type === 'double' ? 2 : 3,
          status: 'pending',
          content: '@cparagraph(5, 10)',
          remark: data.remark || '',
          created_at: new Date().toISOString(),
          updated_at: new Date().toISOString()
        }
        generatedSchemes.push(scheme)
        schemeList.items.unshift(scheme)
      }
      
      schemeList.total += generatedSchemes.length
      
      return {
        code: 200,
        data: generatedSchemes
      }
    }
  },
  
  // 专家管理接口
  {
    url: '/api/expert/list',
    type: 'get',
    response: config => {
      const { page = 1, limit = 20, name, status } = config.query
      let filteredList = expertList.items
      
      if (name) {
        filteredList = filteredList.filter(item => item.name.includes(name))
      }
      if (status) {
        filteredList = filteredList.filter(item => item.status === status)
      }
      
      const start = (page - 1) * limit
      const end = start + parseInt(limit)
      
      return {
        code: 200,
        data: {
          items: filteredList.slice(start, end),
          total: filteredList.length
        }
      }
    }
  },
  
  {
    url: '/api/expert/create',
    type: 'post',
    response: config => {
      const data = JSON.parse(config.body)
      const newExpert = {
        id: expertList.items.length + 1,
        single_example: '',
        double_example: '',
        triple_example: '',
        ...data,
        created_at: new Date().toISOString(),
        updated_at: new Date().toISOString()
      }
      expertList.items.unshift(newExpert)
      expertList.total += 1
      
      return {
        code: 200,
        data: newExpert
      }
    }
  },
  
  {
    url: '/api/expert/update',
    type: 'put',
    response: config => {
      const data = JSON.parse(config.body)
      const index = expertList.items.findIndex(item => item.id === data.id)
      if (index !== -1) {
        expertList.items[index] = { ...expertList.items[index], ...data, updated_at: new Date().toISOString() }
      }
      
      return {
        code: 200,
        data: expertList.items[index]
      }
    }
  },
  
  {
    url: '/api/expert/delete/([0-9]+)',
    type: 'delete',
    response: config => {
      const id = parseInt(config.url.split('/').pop())
      const index = expertList.items.findIndex(item => item.id === id)
      if (index !== -1) {
        expertList.items.splice(index, 1)
        expertList.total -= 1
      }
      
      return {
        code: 200,
        message: '删除成功'
      }
    }
  },
  
  {
    url: '/api/expert/enable/([0-9]+)',
    type: 'post',
    response: config => {
      const id = parseInt(config.url.split('/').pop())
      const index = expertList.items.findIndex(item => item.id === id)
      if (index !== -1) {
        expertList.items[index].status = 'active'
        expertList.items[index].updated_at = new Date().toISOString()
      }
      
      return {
        code: 200,
        message: '启用成功'
      }
    }
  },
  
  {
    url: '/api/expert/disable/([0-9]+)',
    type: 'post',
    response: config => {
      const id = parseInt(config.url.split('/').pop())
      const index = expertList.items.findIndex(item => item.id === id)
      if (index !== -1) {
        expertList.items[index].status = 'inactive'
        expertList.items[index].updated_at = new Date().toISOString()
      }
      
      return {
        code: 200,
        message: '禁用成功'
      }
    }
  },
  
  // 赛事管理接口
  {
    url: '/api/match/list',
    type: 'get',
    response: config => {
      const { page = 1, limit = 20, team_name, league, status } = config.query
      let filteredList = matchList.items
      
      if (team_name) {
        filteredList = filteredList.filter(item => 
          item.home_team.includes(team_name) || item.away_team.includes(team_name)
        )
      }
      if (league) {
        filteredList = filteredList.filter(item => item.league === league)
      }
      if (status) {
        filteredList = filteredList.filter(item => item.status === status)
      }
      
      const start = (page - 1) * limit
      const end = start + parseInt(limit)
      
      return {
        code: 200,
        data: {
          items: filteredList.slice(start, end),
          total: filteredList.length
        }
      }
    }
  },
  
  {
    url: '/api/match/create',
    type: 'post',
    response: config => {
      const data = JSON.parse(config.body)
      const newMatch = {
        id: matchList.items.length + 1,
        ...data,
        created_at: new Date().toISOString(),
        updated_at: new Date().toISOString()
      }
      matchList.items.unshift(newMatch)
      matchList.total += 1
      
      return {
        code: 200,
        data: newMatch
      }
    }
  },
  
  {
    url: '/api/match/update',
    type: 'put',
    response: config => {
      const data = JSON.parse(config.body)
      const index = matchList.items.findIndex(item => item.id === data.id)
      if (index !== -1) {
        matchList.items[index] = { ...matchList.items[index], ...data, updated_at: new Date().toISOString() }
      }
      
      return {
        code: 200,
        data: matchList.items[index]
      }
    }
  },
  
  {
    url: '/api/match/delete/([0-9]+)',
    type: 'delete',
    response: config => {
      const id = parseInt(config.url.split('/').pop())
      const index = matchList.items.findIndex(item => item.id === id)
      if (index !== -1) {
        matchList.items.splice(index, 1)
        matchList.total -= 1
      }
      
      return {
        code: 200,
        message: '删除成功'
      }
    }
  },
  
  {
    url: '/api/match/refresh',
    type: 'post',
    response: config => {
      // 模拟刷新赛事数据
      const newMatches = Mock.mock({
        'items|10': [{
          'id|+1': matchList.items.length + 1,
          'league': '@pick(["英超", "西甲", "德甲", "意甲", "法甲", "欧冠", "欧联"])',
          'home_team': '@cword(2, 4)',
          'away_team': '@cword(2, 4)',
          'match_time': '@datetime',
          'status': '@pick(["upcoming", "live", "finished", "cancelled"])',
          'win_odds|1.5-3.5': 1,
          'draw_odds|2.8-3.5': 1,
          'lose_odds|1.5-3.5': 1,
          'created_at': '@datetime',
          'updated_at': '@datetime'
        }]
      })
      
      matchList.items.unshift(...newMatches.items)
      matchList.total += newMatches.items.length
      
      return {
        code: 200,
        message: '数据刷新成功',
        data: {
          added: newMatches.items.length
        }
      }
    }
  },
  
  {
    url: '/api/match/sync-sporttery',
    type: 'post',
    response: config => {
      // 模拟从体彩API同步数据
      const syncedMatches = Mock.mock({
        'items|15': [{
          'id|+1': matchList.items.length + 1,
          'league': '@pick(["英超", "西甲", "德甲", "意甲", "法甲", "欧冠", "欧联", "中超", "日职", "韩职"])',
          'home_team': '@cword(2, 4)',
          'away_team': '@cword(2, 4)',
          'match_time': '@datetime',
          'status': '@pick(["upcoming", "live", "finished", "cancelled"])',
          'win_odds|1.5-3.5': 1,
          'draw_odds|2.8-3.5': 1,
          'lose_odds|1.5-3.5': 1,
          'sporttery_id': '@integer(100000, 999999)',
          'created_at': '@datetime',
          'updated_at': '@datetime'
        }]
      })
      
      matchList.items.unshift(...syncedMatches.items)
      matchList.total += syncedMatches.items.length
      
      return {
        code: 200,
        message: '体彩数据同步成功',
        data: {
          syncedCount: syncedMatches.items.length,
          matches: syncedMatches.items
        }
      }
    }
  }
]
